"""
This module defines all sample_tracker models. these models are direct
representation of real objects and concepts used in a neutron facility.

:author: Gervaise Alina
:version: 0.1
"""


from django.db import models
from django.contrib.auth.models import User


class Address(models.Model):
    """
    The address of the user institution or lab.
    :Note: Think about how address are described in different
            countries
    """
    street = models.CharField(max_length=100)
    state = models.CharField(max_length=100)
    province = models.CharField(max_length=100)
    city = models.CharField(max_length=100)
    zip_code =  models.IntegerField()
    country = models.CharField(max_length=100)
    
    
class Instrument(models.Model):
    """
    Instrument used for measurements
    """
    name = models.CharField(max_length=30)
    
class Experiment(models.Model):
    """
    Series of measurements performed on the sample 
    at the given instrument.
    """
    # Experiment id  are given by ims now
    experiment_id = models.CharField(max_length=100)
    #the rate of neutrons shot of the sample neutron/cm2/s
    flux_on_sample = models.FloatField()
    # Instrument used
    instrument = models.ForeignKey(Instrument)
    # The beginning of the experiment
    start_date  = models.DateTimeField()
    # The End of the experiment
    stop_date = models.DateTimeField()
   
class Sample(models.Model):
    """
    Chemical component to measure
    :example:    Chemical formula = MnO
                            name  = Manganese Oxide
    """
    user = models.ForeignKey(User)
    chemical_formula = models.CharField(max_length=30)
    name = models.CharField(max_length=30)
    msds =  models.CharField(max_length=30)
    mass = models.FloatField()
    volume = models.FloatField()
    bar_code = models.CharField(max_length=100)
    experiment_start_time = models.DateTimeField('Experiment Beginning',
                                                    blank=True, null=True)
    experiment_end_time = models.DateTimeField('Experiment End', 
                                               blank=True, null=True)

    def __str__(self):
        return self.name
    
    def __unicode__(self):
        return self.name
    
    class Meta:
        ordering = ["-name", 'experiment_end_time']

    class Admin:
        pass


class Hazard(models.Model):
    """
    Given details on how poisonous the sample is.
    """
    # (ex. poisonous, toxic, etc.)
    name = models.CharField(max_length=30)
    # text blob about the hazards of the sample
    msds =  models.CharField(max_length=30)
    
    def __str__(self):
        return self.name
    
    def __unicode__(self):
        return self.name
    
   
class Photo(models.Model):
    """
    Photo related to a sample and a given experiment
    :Note: the caption might store in the image itself
    """
    image = models.FilePathField()
    caption  = models.CharField(max_length=30)
    experiment = models.ForeignKey(Experiment)
    # multiple photos for one sample
    sample = models.ForeignKey(Sample)
    
    def __unicode__(self):
        return self.caption
    

class StorageArea(models.Model):
    """
    """
    # example building name C100, G100, Radlochemistry
    name  = models.CharField(max_length=30)
    coordinates = models.CharField(max_length=30)
    
    def __unicode__(self):
        return self.name
        
class Locker(models.Model):
    """
    """
    coordinates = models.CharField(max_length=30)
    storage_area =  models.ForeignKey(StorageArea)
    name  = models.CharField(max_length=30)
    #all lockers should have a number
    number = models.IntegerField()
    
    def __unicode__(self):
        return self.name
    
class Location(models.Model):
    """
    
    """
    # coordinate recorded by RFID
    coordinates  = models.CharField(max_length=30)
    locker = models.ForeignKey(Locker)
    shelf_number = models.IntegerField()
    name  = models.CharField(max_length=30)
    
    def __unicode__(self):
        return self.shelf_number

    


